using System;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using ns20;

namespace ns15
{
	internal class Class68
	{
		private static readonly Encoding encoding_0 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);

		private readonly BinaryWriter binaryWriter_0;

		private byte[] byte_0;

		[CompilerGenerated]
		private DateTimeKind dateTimeKind_0;

		public DateTimeKind DateTimeKind_0
		{
			[CompilerGenerated]
			get
			{
				return dateTimeKind_0;
			}
			[CompilerGenerated]
			set
			{
				dateTimeKind_0 = value;
			}
		}

		public Class68(BinaryWriter writer)
		{
			DateTimeKind_0 = DateTimeKind.Utc;
			binaryWriter_0 = writer;
		}

		public void method_0()
		{
			binaryWriter_0.Flush();
		}

		public void method_1()
		{
			binaryWriter_0.Close();
		}

		public void method_2(Class71 class71_0)
		{
			method_8(class71_0);
			method_3(class71_0);
		}

		private void method_3(Class71 class71_0)
		{
			switch (class71_0.Enum11_0)
			{
			case Enum11.Number:
			{
				Class74 class8 = (Class74)class71_0;
				binaryWriter_0.Write(Convert.ToDouble(class8.Object_0, CultureInfo.InvariantCulture));
				break;
			}
			case Enum11.String:
			{
				Class75 class6 = (Class75)class71_0;
				method_4((string)class6.Object_0, class6.Int32_1, class6.Int32_0 - 4);
				break;
			}
			case Enum11.Object:
			{
				Class72 class7 = (Class72)class71_0;
				binaryWriter_0.Write(class7.Int32_0);
				foreach (Class78 item in class7)
				{
					binaryWriter_0.Write((sbyte)item.Class71_0.Enum11_0);
					method_4((string)item.Name.Object_0, item.Name.Int32_1, null);
					method_3(item.Class71_0);
				}
				binaryWriter_0.Write((byte)0);
				break;
			}
			case Enum11.Array:
			{
				Class73 class9 = (Class73)class71_0;
				binaryWriter_0.Write(class9.Int32_0);
				ulong num2 = 0uL;
				foreach (Class71 item2 in class9)
				{
					binaryWriter_0.Write((sbyte)item2.Enum11_0);
					method_4(num2.ToString(CultureInfo.InvariantCulture), Class193.smethod_0(num2), null);
					method_3(item2);
					num2++;
				}
				binaryWriter_0.Write((byte)0);
				break;
			}
			case Enum11.Binary:
			{
				Class76 class10 = (Class76)class71_0;
				byte[] array = (byte[])class10.Object_0;
				binaryWriter_0.Write(array.Length);
				binaryWriter_0.Write((byte)class10.Enum9_0);
				binaryWriter_0.Write(array);
				break;
			}
			case Enum11.Oid:
			{
				Class74 class5 = (Class74)class71_0;
				byte[] buffer = (byte[])class5.Object_0;
				binaryWriter_0.Write(buffer);
				break;
			}
			case Enum11.Boolean:
			{
				Class74 class11 = (Class74)class71_0;
				binaryWriter_0.Write((bool)class11.Object_0);
				break;
			}
			case Enum11.Date:
			{
				Class74 class4 = (Class74)class71_0;
				long num = 0L;
				if (class4.Object_0 is DateTime)
				{
					DateTime dateTime_ = (DateTime)class4.Object_0;
					if (DateTimeKind_0 == DateTimeKind.Utc)
					{
						dateTime_ = dateTime_.ToUniversalTime();
					}
					else if (DateTimeKind_0 == DateTimeKind.Local)
					{
						dateTime_ = dateTime_.ToLocalTime();
					}
					num = Class185.smethod_9(dateTime_, bool_0: false);
				}
				else
				{
					DateTimeOffset dateTimeOffset = (DateTimeOffset)class4.Object_0;
					num = Class185.smethod_7(dateTimeOffset.UtcDateTime, dateTimeOffset.Offset);
				}
				binaryWriter_0.Write(num);
				break;
			}
			case Enum11.Undefined:
			case Enum11.Null:
				break;
			case Enum11.Regex:
			{
				Class77 class3 = (Class77)class71_0;
				method_4((string)class3.Class75_0.Object_0, class3.Class75_0.Int32_1, null);
				method_4((string)class3.Class75_1.Object_0, class3.Class75_1.Int32_1, null);
				break;
			}
			case Enum11.Integer:
			{
				Class74 class2 = (Class74)class71_0;
				binaryWriter_0.Write(Convert.ToInt32(class2.Object_0, CultureInfo.InvariantCulture));
				break;
			}
			default:
				throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".smethod_0(CultureInfo.InvariantCulture, class71_0.Enum11_0));
			case Enum11.Long:
			{
				Class74 @class = (Class74)class71_0;
				binaryWriter_0.Write(Convert.ToInt64(@class.Object_0, CultureInfo.InvariantCulture));
				break;
			}
			}
		}

		private void method_4(string string_0, int int_0, int? nullable_0)
		{
			if (nullable_0.HasValue)
			{
				binaryWriter_0.Write(nullable_0.Value);
			}
			method_5(string_0, int_0);
			binaryWriter_0.Write((byte)0);
		}

		public void method_5(string string_0, int int_0)
		{
			if (string_0 != null)
			{
				if (byte_0 == null)
				{
					byte_0 = new byte[256];
				}
				if (int_0 <= 256)
				{
					encoding_0.GetBytes(string_0, 0, string_0.Length, byte_0, 0);
					binaryWriter_0.Write(byte_0, 0, int_0);
				}
				else
				{
					byte[] bytes = encoding_0.GetBytes(string_0);
					binaryWriter_0.Write(bytes);
				}
			}
		}

		private int method_6(int int_0)
		{
			return int_0 + 1;
		}

		private int method_7(int int_0, bool bool_0)
		{
			int num = ((!bool_0) ? 1 : 5);
			return num + int_0;
		}

		private int method_8(Class71 class71_0)
		{
			switch (class71_0.Enum11_0)
			{
			case Enum11.Number:
				return 8;
			case Enum11.String:
			{
				Class75 class4 = (Class75)class71_0;
				string text = (string)class4.Object_0;
				class4.Int32_1 = ((text != null) ? encoding_0.GetByteCount(text) : 0);
				class4.Int32_0 = method_7(class4.Int32_1, class4.Boolean_0);
				return class4.Int32_0;
			}
			case Enum11.Object:
			{
				Class72 class5 = (Class72)class71_0;
				int num6 = 4;
				foreach (Class78 item in class5)
				{
					int num7 = 1;
					num7 = 1 + method_8(item.Name);
					num7 += method_8(item.Class71_0);
					num6 += num7;
				}
				return class5.Int32_0 = num6 + 1;
			}
			case Enum11.Array:
			{
				Class73 class3 = (Class73)class71_0;
				int num3 = 4;
				ulong num4 = 0uL;
				foreach (Class71 item2 in class3)
				{
					num3++;
					num3 += method_6(Class193.smethod_0(num4));
					num3 += method_8(item2);
					num4++;
				}
				num3 = (class3.Int32_0 = num3 + 1);
				return class3.Int32_0;
			}
			case Enum11.Binary:
			{
				Class76 class2 = (Class76)class71_0;
				byte[] array = (byte[])class2.Object_0;
				class2.Int32_0 = 5 + array.Length;
				return class2.Int32_0;
			}
			case Enum11.Oid:
				return 12;
			case Enum11.Boolean:
				return 1;
			case Enum11.Date:
				return 8;
			case Enum11.Undefined:
			case Enum11.Null:
				return 0;
			case Enum11.Regex:
			{
				Class77 @class = (Class77)class71_0;
				int num = 0;
				num = 0 + method_8(@class.Class75_0);
				num = (@class.Int32_0 = num + method_8(@class.Class75_1));
				return @class.Int32_0;
			}
			case Enum11.Integer:
				return 4;
			default:
				throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".smethod_0(CultureInfo.InvariantCulture, class71_0.Enum11_0));
			case Enum11.Long:
				return 8;
			}
		}
	}
}
